package com.kuangqiyuan.yatzygame.rule;

import com.kuangqiyuan.yatzygame.Play;

/**
 * 处理下面的几种规则
 * - 两个相同
 * - 三个相同
 * - 四个相同
 * - 全部相同
 */
public class PairsRuleHandler extends AbstractRuleHandler {
  public PairsRuleHandler() {
    super();
  }

  @Override
  public int Count() {
    int[] board = new int[6];
    for (int i = 0; i < 6; i++) {
      board[i] = 0;
    }

    for (int i = 0, m; i < 5; i++) {
      m = this.play.getRecord(i);
      board[m - 1]++;
    }

    int topScore = 0, thisScore;
    for (int i = 0, x = 0; i < 6; i++) {
      if (board[i] < 2) {
        continue;
      }

      // 全部相同
      if (board[i] == 5) {
        return 50;
      }

      // 直接以分数比较。1 * 4 < 2 * 3。不能以重复次数比较
      thisScore = (i + 1) * board[i];
      if (topScore < thisScore) {
        topScore = thisScore;
      }
    }

//    System.out.println(String.format("%s counts: %d", this.getClass(), topScore));

    return topScore;
  }
}
